<?php
namespace app\common\logic;

use app\common\exception\FailException;
use app\common\model\User as UserModel;
use app\common\model\Shop as ShopModel;
use fast\Random;
use think\Controller;
use think\Db;
use think\Exception;

class Pay extends Controller
{
    public function pay($userid,$shopid,$money,$type){
        $userinfo=UserModel::where("id",$userid)->where("status",1)->find();
        $money=abs($money);
        if(!$userinfo) {
            throw new FailException(['code' => 201,"msg"=>"用户不存在","errorCode"=>300]);
            die();
        }
        //计算税费
        $new_tax_fl=Db::table("t_config")->where("name","new_tax")->value("data");
        $tax_money=  floor($new_tax_fl*$money*100)/100;

        //计算服务费
        $new_service_fl=Db::table("t_config")->where("name","new_service")->value("data");
        $service_money=  floor($new_service_fl*$money*100)/100;

        //合计
        $total_money=$money+$tax_money+$service_money;

        $shop = ShopModel::where("id",$shopid)->where("status",1)->find();

        if(!$shop){
            throw new FailException( ['code' => 201,"msg"=>"商家正在维护暂不能支付，请联系客服","errorCode"=>404]);

        }


        Db::startTrans();    //开启事务
        try{
            $usercapital=Db::table("t_capital")->where("uid",$userid)->where("type",2)->where("status",1)->lock(true)->find();
            if($type==1){
                if((float)$usercapital['money']<=(float)$total_money){

                    throw new FailException( ['code' => 201,"msg"=>"用户资金不足","errorCode"=>300]);
                }
            }
            $shopcapital=Db::table("t_capital")->where("uid",$shopid)->where("type",1)->where("status",1)->lock(true)->find();

            $total_money=abs($total_money);
            $zf_total_money=0-$total_money;

            $usercapital_log="账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,其中税费".$tax_money."元,服务费".$service_money."元";
            $usercapital_log2="账号：".$userinfo['username']." 向 ".$shop['name']." 支付了现金 ".$total_money."元,其中税费".$tax_money."元,服务费".$service_money."元";

            $sbz=$shop['name']." 收入 ".$money;

            $usermoney=$usercapital['money']-$total_money;

            $shopmoney=$shopcapital['money']+$money;

            $order_add_info = [
                'tel'=>$userinfo['mobile'],
                'user_id' => $userid,
                'shop_id' => $shopid,
                'sn'=>Random::build("nozero",16),
                'area_id' => $shop['area_id'],
                'address'=>$shop['address'],
                'real_price'=>abs($money),
                'total_price'=>abs($total_money),
                'tax_price'=>$tax_money,
                'service_price'=>$service_money,
                'status'=>1,
                'created_at'=>date("Y-m-d H:i:s",time()),
                'updated_at'=>date("Y-m-d H:i:s",time()),
                'type'=>$type
            ];

            $orderid=Db::table('t_order')->insertGetId($order_add_info);

            if($type==1){   //余额支付
                $usercapital_water_data = [
                    'cid'=>$usercapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $zf_total_money,
                    'type'=>1,
                    'addtime'=>time(),
                    'name'=>$usercapital_log,
                    'order_id'=>$orderid
                ];

                Db::table('t_capital_water')->insertGetId($usercapital_water_data);

                Db::table('t_capital')->where('uid',$userid) ->where("type",2)->where("status",1)->update([
                    'money'=>$usermoney,
                    'consumer'=>$usercapital['consumer']+$total_money
                ]);
            }elseif($type==2){ //现金支付

                $usercapital_water_data = [
                    'cid'=>$usercapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $zf_total_money,
                    'type'=>6,
                    'addtime'=>time(),
                    'name'=>$usercapital_log2,
                    'order_id'=>$orderid
                ];
                Db::table('t_capital_water')->insertGetId($usercapital_water_data);

            }else{
                throw new FailException( ['code' => 201,"msg"=>"该支付方式正在维护中","errorCode"=>300]);
            }
            //创建订单


            //商家获取金额
            $shopcapital_water_data = [
                'cid'=>$shopcapital['id'],
                'uid' => $userid,
                'shopid'=>$shopid,
                'money' => $money,
                'type'=>2,
                'addtime'=>time(),
                'name'=>$sbz,
                'order_id'=>$orderid
            ];
            Db::table('t_capital_water')->insertGetId($shopcapital_water_data);

            Db::table('t_capital')->where('uid',$shopid) ->where("type",1)->where("status",1)->update([
                'money'=>$shopmoney,
            ]);



            Db::commit();   //提交事务

        }catch(FailException $e){
            Db::rollback(); //回滚事务
            throw new FailException(['code' => 201,"msg"=>$e->msg,"errorCode"=>405]);
            die();
        }


        $bldata1 = Db::table("t_config")->where("name", "new_fh_gd")->value("data");
        $bldata2 = Db::table("t_config")->where("name", "new_cz_bl")->value("data");
        $re1 = Pay::share_out_bonus($userid, $money, $bldata1, $userid,1,$orderid);
        if ($re1 > 1) {
            Pay::share_out_bonus($re1, $money, $bldata2, $userid,2,$orderid);
        }

        return ["code"=>200,"msg"=>"成功！","data"=>$orderid];

    }


    public function pay_by($userid,$shopid,$money,$type){
        $userinfo=UserModel::where("id",$userid)->where("status",1)->find();
        $money=abs($money);
        if(!$userinfo) {
            throw new FailException(['code' => 201,"msg"=>"用户不存在","errorCode"=>300]);
            die();
        }
        //计算税费
        $new_tax_fl=Db::table("t_config")->where("name","new_tax")->value("data");
        $tax_money=  floor($new_tax_fl*$money*100)/100;

        //计算服务费
        $new_service_fl=Db::table("t_config")->where("name","new_service")->value("data");
        $service_money=  floor($new_service_fl*$money*100)/100;

        //合计
        $total_money=$money+$tax_money+$service_money;

        $shop = ShopModel::where("id",$shopid)->where("status",1)->find();

        if(!$shop){
            throw new FailException( ['code' => 201,"msg"=>"商家正在维护暂不能支付，请联系客服","errorCode"=>404]);

        }

        Db::startTrans();    //开启事务
        try{
            $usercapital=Db::table("t_capital")->where("uid",$userid)->where("type",2)->where("status",1)->lock(true)->find();
            if($type==1){
                if((float)$usercapital['money']<=(float)$total_money){

                    throw new FailException( ['code' => 201,"msg"=>"用户资金不足","errorCode"=>300]);
                }
            }
            $shopcapital=Db::table("t_capital")->where("uid",$shopid)->where("type",1)->where("status",1)->lock(true)->find();

            $total_money=abs($total_money);
            $zf_total_money=0-$total_money;

            $usercapital_log="账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,其中税费".$tax_money."元,服务费".$service_money."元";
            $usercapital_log2="账号：".$userinfo['username']." 向 ".$shop['name']." 支付了现金 ".$total_money."元,其中税费".$tax_money."元,服务费".$service_money."元";

            $sbz=$shop['name']." 收入 ".$money;

            $usermoney=$usercapital['money']-$total_money;

            $shopmoney=$shopcapital['money']+$money;

            $order_sn=Random::build("nozero",16);


            if($type==1){   //余额支付
                $order_add_info = [
                    'tel'=>$userinfo['mobile'],
                    'user_id' => $userid,
                    'shop_id' => $shopid,
                    'sn'=>$order_sn,
                    'area_id' => $shop['area_id'],
                    'address'=>$shop['address'],
                    'real_price'=>abs($money),
                    'total_price'=>abs($total_money),
                    'tax_price'=>$tax_money,
                    'service_price'=>$service_money,
                    'status'=>1,
                    'created_at'=>date("Y-m-d H:i:s",time()),
                    'updated_at'=>date("Y-m-d H:i:s",time()),
                    'type'=>$type
                ];

                $orderid=Db::table('t_order')->insertGetId($order_add_info);
                $usercapital_water_data = [
                    'cid'=>$usercapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $zf_total_money,
                    'type'=>1,
                    'addtime'=>time(),
                    'name'=>$usercapital_log,
                    'order_id'=>$orderid
                ];

                Db::table('t_capital_water')->insertGetId($usercapital_water_data);

                Db::table('t_capital')->where('uid',$userid) ->where("type",2)->where("status",1)->update([
                    'money'=>$usermoney,
                    'consumer'=>$usercapital['consumer']+$total_money
                ]);

                $shopcapital_water_data = [
                    'cid'=>$shopcapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $money,
                    'type'=>2,
                    'addtime'=>time(),
                    'name'=>$sbz,
                    'order_id'=>$orderid
                ];
                Db::table('t_capital_water')->insertGetId($shopcapital_water_data);

                Db::table('t_capital')->where('uid',$shopid) ->where("type",1)->where("status",1)->update([
                    'money'=>$shopmoney,
                ]);

                //二级代理商加钱
                if($userinfo['agent_id']){
                    $level2agent= Db::table("t_user")->where("id",$userinfo['agent_id'])->where("status",1)->where("is_agent",1)->find();

                    if($level2agent){
                        $agentcaptail= Db::table("t_capital")->where("type",2)->where("uid",$userinfo['agent_id'])->where("status",1)->find(); //二级代理
                        if($agentcaptail){
                            $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                            //代理总金额
                            $agent_money=floor($money*$agent_fh*100)/100;
                            //二级代理获得费用
                            $level2agent_money= floor($agent_money*$level2agent['agent']*100)/100;

                            Db::table("t_capital")->where("type",2)->where("uid",$userinfo['agent_id'])->where("status",1)->update([
                                "agent_money"=>$agentcaptail['agent_money']+$level2agent_money,
                                "count_agent_money"=>$agentcaptail['count_agent_money']+$level2agent_money,
                            ]);

                            Db::table("t_capital_water")->insertGetId([
                                'cid'=>$agentcaptail['id'],
                                'uid' => $userinfo['agent_id'],
                                'money' => $level2agent_money,
                                'type'=>15,
                                'addtime'=>time(),
                                'name'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理费".$level2agent_money."元",
                                "b_username" => $userinfo['username'],
                                "b_money" => $money,
                                'order_id'=>$orderid
                            ]);



                        }

                        if($userinfo['top_agent_id']){

                            $level1agent= Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->find();

                            if($level1agent){

                                $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                                //代理总金额
                                $agent_money=floor($money*$agent_fh*100)/100;
                                //二级代理获得费用
                                $level2agent_money= floor($agent_money*$level2agent['agent']*100)/100;

                                $level1agent_money=$agent_money-$level2agent_money;

                                Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->update([

                                    "count_money"=>$level1agent['count_money']+$level1agent_money,
                                    "money"=>$level1agent['money']+$level1agent_money,


                                ]);

                                Db::table("t_agent_log")->insertGetId([
                                    'buid'=>$userid,
                                    'uid' => $userinfo['top_agent_id'],
                                    'money' => $level1agent_money,
                                    'aid'=>$userinfo['top_agent_id'],
                                    'addtime'=>time(),
                                    'desc'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理金".$level2agent_money."元",
                                    'order_id'=>$orderid,
                                    "b_money" => $money,
                                ]);



                            }


                        }



                    }

                }else{

                    if($userinfo['top_agent_id']){

                        $level1agent= Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->find();

                        if($level1agent){

                            $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                            //代理总金额
                            $agent_money=floor($money*$agent_fh*100)/100;
                            //二级代理获得费用


                            $level1agent_money=$agent_money;

                            Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->update([

                                "count_money"=>$level1agent['count_money']+$level1agent_money,
                                "money"=>$level1agent['money']+$level1agent_money,


                            ]);

                            Db::table("t_agent_log")->insertGetId([
                                'buid'=>$userid,
                                'uid' => $userinfo['top_agent_id'],
                                'money' => $level1agent_money,
                                'aid'=>$userid,
                                'addtime'=>time(),
                                'desc'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理费".$level1agent_money."元",
                                'order_id'=>$orderid,
                                "b_money" => $money,
                            ]);



                        }


                    }

                }


            }elseif($type==2){ //现金支付
                $order_add_info = [
                    'tel'=>$userinfo['mobile'],
                    'user_id' => $userid,
                    'shop_id' => $shopid,
                    'sn'=>$order_sn,
                    'area_id' => $shop['area_id'],
                    'address'=>$shop['address'],
                    'real_price'=>abs($money),
                    'total_price'=>abs($total_money),
                    'tax_price'=>$tax_money,
                    'service_price'=>$service_money,
                    'status'=>1,
                    'created_at'=>date("Y-m-d H:i:s",time()),
                    'updated_at'=>date("Y-m-d H:i:s",time()),
                    'type'=>$type
                ];

                $orderid=Db::table('t_order')->insertGetId($order_add_info);
                $usercapital_water_data = [
                    'cid'=>$usercapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $zf_total_money,
                    'type'=>6,
                    'addtime'=>time(),
                    'name'=>$usercapital_log2,
                    'order_id'=>$orderid
                ];
                Db::table('t_capital_water')->insertGetId($usercapital_water_data);

                $shopcapital_water_data = [
                    'cid'=>$shopcapital['id'],
                    'uid' => $userid,
                    'shopid'=>$shopid,
                    'money' => $money,
                    'type'=>2,
                    'addtime'=>time(),
                    'name'=>$sbz,
                    'order_id'=>$orderid
                ];
                Db::table('t_capital_water')->insertGetId($shopcapital_water_data);

                Db::table('t_capital')->where('uid',$shopid) ->where("type",1)->where("status",1)->update([
                    'money'=>$shopmoney,
                ]);

            }elseif($type==3 || $type==4){ //支付宝支付
                $order_add_info = [
                    'tel'=>$userinfo['mobile'],
                    'user_id' => $userid,
                    'shop_id' => $shopid,
                    'sn'=>$order_sn,
                    'area_id' => $shop['area_id'],
                    'address'=>$shop['address'],
                    'real_price'=>abs($money),
                    'total_price'=>abs($total_money),
                    'tax_price'=>$tax_money,
                    'service_price'=>$service_money,
                    'status'=>0,
                    'created_at'=>date("Y-m-d H:i:s",time()),
                    'updated_at'=>date("Y-m-d H:i:s",time()),
                    'type'=>$type
                ];

                $orderid=Db::table('t_order')->insertGetId($order_add_info);
//                $usercapital_water_data = [
//                    'cid'=>$usercapital['id'],
//                    'uid' => $userid,
//                    'shopid'=>$shopid,
//                    'money' => $zf_total_money,
//                    'type'=>6,
//                    'addtime'=>time(),
//                    'name'=>$usercapital_log2,
//                    'order_id'=>$orderid
//                ];
//                Db::table('t_capital_water')->insertGetId($usercapital_water_data);

            }else{
                throw new FailException( ['code' => 201,"msg"=>"该支付方式正在维护中","errorCode"=>300]);
            }
            //创建订单


            //商家获取金额


            Db::commit();   //提交事务

        }catch(FailException $e){
            Db::rollback(); //回滚事务
            throw new FailException(['code' => 201,"msg"=>$e->msg,"errorCode"=>405]);
            die();
        }

        if($type==1 || $type==2) { //现金支付
            $bldata1 = Db::table("t_config")->where("name", "new_fh_gd")->value("data");
            $bldata2 = Db::table("t_config")->where("name", "new_cz_bl")->value("data");
            $re1 = Pay::share_out_bonus($userid, $money, $bldata1, $userid, 1, $orderid,$shop['proportion']);
            if ($re1 > 1) {
                Pay::share_out_bonus($re1, $money, $bldata2, $userid, 2, $orderid,$shop['proportion']);
            }
            $bldata3=Db::table("t_config")->where("name","recharge_fh")->value("data");
            $bldata4=Db::table("t_config")->where("name","recharge_cz")->value("data");
            $re2=Recharge::share_out_bonus($userid,$money,$bldata3,$userid,1,$orderid,$shop['proportion']);
            if($re2>1){
                Recharge::share_out_bonus($re1,$money,$bldata4,$userid,2,$orderid,$shop['proportion']);

            }
        }

        return ["code"=>200,"msg"=>"成功！","data"=>$orderid,"order_sn"=>$order_sn,"zfmoney"=>$total_money];

    }

    /**
     * @param $out_trade_no
     * @param $trade_no
     * @return array
     * @throws Exception
     * @throws FailException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 微信支付宝回调
     */
    public function alipay_notify($out_trade_no,$trade_no){
        $orderdata=\app\common\model\Order::where("sn",$out_trade_no)->find();
        if($orderdata["status"]==1){
            throw new FailException(['code' => 201,"msg"=>"已经支付过了请勿非法支付","errorCode"=>404]);
            die();
        }
        $userid=$orderdata['user_id'];
        $shopid=$orderdata['shop_id'];
        $money=$orderdata['real_price'];  //订单真实费用
        $type=3;

        $userinfo=UserModel::where("id",$userid)->where("status",1)->find();
        $money=abs($money);
        if(!$userinfo) {
            throw new FailException(['code' => 201,"msg"=>"用户不存在","errorCode"=>300]);
            die();
        }
        //计算税费
        $new_tax_fl=Db::table("t_config")->where("name","new_tax")->value("data");
        $tax_money=  floor($new_tax_fl*$money*100)/100;

        //计算服务费
        $new_service_fl=Db::table("t_config")->where("name","new_service")->value("data");
        $service_money=  floor($new_service_fl*$money*100)/100;

        //合计
        $total_money=$money+$tax_money+$service_money;

        $shop = ShopModel::where("id",$shopid)->where("status",1)->find();

        if(!$shop){
            throw new FailException( ['code' => 201,"msg"=>"商家正在维护暂不能支付，请联系客服","errorCode"=>404]);
        }

        Db::startTrans();    //开启事务
        try{
            $usercapital=Db::table("t_capital")->where("uid",$userid)->where("type",2)->where("status",1)->lock(true)->find();
            if($type==1){
                if((float)$usercapital['money']<=(float)$total_money){

                    throw new FailException( ['code' => 201,"msg"=>"用户资金不足","errorCode"=>300]);
                }
            }
            $shopcapital=Db::table("t_capital")->where("uid",$shopid)->where("type",1)->where("status",1)->lock(true)->find();

            $total_money=abs($total_money);
            $zf_total_money=0-$total_money;

            $usercapital_log="账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,其中税费".$tax_money."元,服务费".$service_money."元";

            $sbz=$shop['name']." 收入 ".$money;

            // $usermoney=$usercapital['money']-$total_money;

            $shopmoney=$shopcapital['money']+$money;

            $order_sn=Random::build("nozero",16);


            $order_up_info = [

                'status'=>1,
                'alipay_trade_no'=>$trade_no
            ];

            Db::table('t_order')->where("sn",$out_trade_no)->update($order_up_info);
            $usercapital_water_data = [
                'cid'=>$usercapital['id'],
                'uid' => $userid,
                'shopid'=>$shopid,
                'money' => $zf_total_money,
                'type'=>1,
                'addtime'=>time(),
                'name'=>$usercapital_log,
                'order_id'=>$orderdata['id']
            ];

            Db::table('t_capital_water')->insertGetId($usercapital_water_data);

            Db::table('t_capital')->where('uid',$userid) ->where("type",2)->where("status",1)->update([
//                'money'=>$usermoney,
                'consumer'=>$usercapital['consumer']+$total_money
            ]);

            $shopcapital_water_data = [
                'cid'=>$shopcapital['id'],
                'uid' => $userid,
                'shopid'=>$shopid,
                'money' => $money,
                'type'=>2,
                'addtime'=>time(),
                'name'=>$sbz,
                'order_id'=>$orderdata['id']
            ];
            Db::table('t_capital_water')->insertGetId($shopcapital_water_data);

            Db::table('t_capital')->where('uid',$shopid) ->where("type",1)->where("status",1)->update([
                'money'=>$shopmoney,
            ]);

            //二级代理商加钱
            if($userinfo['agent_id']){
                $level2agent= Db::table("t_user")->where("id",$userinfo['agent_id'])->where("status",1)->where("is_agent",1)->find();

                if($level2agent){
                    $agentcaptail= Db::table("t_capital")->where("type",2)->where("uid",$userinfo['agent_id'])->where("status",1)->find(); //二级代理
                    if($agentcaptail){
                        $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                        //代理总金额
                        $agent_money=floor($money*$agent_fh*100)/100;
                        //二级代理获得费用
                        $level2agent_money= floor($agent_money*$level2agent['agent']*100)/100;

                        Db::table("t_capital")->where("type",2)->where("uid",$userinfo['agent_id'])->where("status",1)->update([
                            "agent_money"=>$agentcaptail['agent_money']+$level2agent_money,
                            "count_agent_money"=>$agentcaptail['count_agent_money']+$level2agent_money,
                        ]);

                        Db::table("t_capital_water")->insertGetId([
                            'cid'=>$agentcaptail['id'],
                            'uid' => $userinfo['agent_id'],
                            'money' => $level2agent_money,
                            'type'=>15,
                            'addtime'=>time(),
                            'name'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理费".$level2agent_money."元",
                            "b_username" => $userinfo['username'],
                            "b_money" => $money,
                            'order_id'=>$orderdata['id']
                        ]);



                    }

                    if($userinfo['top_agent_id']){

                        $level1agent= Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->find();

                        if($level1agent){

                            $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                            //代理总金额
                            $agent_money=floor($money*$agent_fh*100)/100;
                            //二级代理获得费用
                            $level2agent_money= floor($agent_money*$level2agent['agent']*100)/100;

                            $level1agent_money=$agent_money-$level2agent_money;

                            Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->update([

                                "count_money"=>$level1agent['count_money']+$level1agent_money,
                                "money"=>$level1agent['money']+$level1agent_money,


                            ]);

                            Db::table("t_agent_log")->insertGetId([
                                'buid'=>$userid,
                                'uid' => $userinfo['top_agent_id'],
                                'money' => $level1agent_money,
                                'aid'=>$userinfo['top_agent_id'],
                                'addtime'=>time(),
                                'desc'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理金".$level2agent_money."元",
                                'order_id'=>$orderdata['id'],
                                "b_money" => $money,
                            ]);



                        }


                    }



                }

            }else{

                if($userinfo['top_agent_id']){

                    $level1agent= Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->find();

                    if($level1agent){

                        $agent_fh=Db::table("t_config")->where("name","agent_fh")->value("data");
                        //代理总金额
                        $agent_money=floor($money*$agent_fh*100)/100;
                        //二级代理获得费用


                        $level1agent_money=$agent_money;

                        Db::table("t_agent")->where("id",$userinfo['top_agent_id'])->where("status",1)->update([

                            "count_money"=>$level1agent['count_money']+$level1agent_money,
                            "money"=>$level1agent['money']+$level1agent_money,


                        ]);

                        Db::table("t_agent_log")->insertGetId([
                            'buid'=>$userid,
                            'uid' => $userinfo['top_agent_id'],
                            'money' => $level1agent_money,
                            'aid'=>$userid,
                            'addtime'=>time(),
                            'desc'=>"账号：".$userinfo['username']." 向 ".$shop['name']." 支付了 ".$total_money."元,获取代理费".$level1agent_money."元",
                            'order_id'=>$orderdata['id'],
                            "b_money" => $money,
                        ]);



                    }


                }

            }



            Db::commit();   //提交事务

        }catch(FailException $e){
            Db::rollback(); //回滚事务
            throw new FailException(['code' => 201,"msg"=>$e->msg,"errorCode"=>405]);
            die();
        }

        $bldata1 = Db::table("t_config")->where("name", "new_fh_gd")->value("data");
        $bldata2 = Db::table("t_config")->where("name", "new_cz_bl")->value("data");
        $re1 = Pay::share_out_bonus($userid, $money, $bldata1, $userid, 1, $orderdata['id'],$shop['proportion']);
        if ($re1 > 1) {
            Pay::share_out_bonus($re1, $money, $bldata2, $userid, 2, $orderdata['id'],$shop['proportion']);
        }

        $bldata3=Db::table("t_config")->where("name","recharge_fh")->value("data");
        $bldata4=Db::table("t_config")->where("name","recharge_cz")->value("data");
        $re2=Recharge::share_out_bonus($userid,$money,$bldata3,$userid,1,$orderdata['id'],$shop['proportion']);
        if($re2>1){
            Recharge::share_out_bonus($re1,$money,$bldata4,$userid,2,$orderdata['id'],$shop['proportion']);

        }


        return ["code"=>200,"msg"=>"成功！","data"=>$orderdata['id'],"order_sn"=>$order_sn];

    }

    public function yy_pay_password($userid,$password){

        $userinfo=UserModel::where("id",$userid)->where("status",1)->find();

        if(!$userinfo) {
            return false;
        }else{
            if(!$userinfo['pay_password']){

                throw new FailException(["code"=>201,"msg"=>"支付密码未设置","errorCode"=>432]);
            }
            if($password!=$userinfo['pay_password']){
                return false;
            }else{
                return true;
            }
        }



    }

    public static function share_out_bonus($uid,$money,$bldata1,$yuid,$level,$orderid,$proportion=0.7){
        Db::startTrans();    //开启事务
        try{
            $pid = Db::table("t_user")->where("id",$uid)->value("pid");
            $money=abs($money);
            $dz=floor($money*(1-$proportion)*$bldata1*100/30*100)/100;
            if($pid){

                $username = Db::table("t_user")->where("id",$yuid)->value("username");
                $refereedata = Db::table("t_user")->where("id",$pid)->find();

                if(!$refereedata){

                    Db::table("t_overflow_log")->insert([
                        "uid"=>$pid,
                        "money"=>$dz,
                        "type"=>1,
                        "addtime"=>time(),
                        'created_at'=>date("Y-m-d H:i:s",time()),
                        'updated_at'=>date("Y-m-d H:i:s",time())
                    ]);
                }
                $capitalc=Db::table("t_capital")->where("type",2)->where("uid",$pid)->lock(true)->find();

                if($refereedata['status']!=1||$refereedata['role_id']==0){

                    Db::table("t_overflow_log")->insert([
                        "uid"=>$pid,
                        "money"=>$dz,
                        "type"=>1,
                        "addtime"=>time(),
                        'created_at'=>date("Y-m-d H:i:s",time()),
                        'updated_at'=>date("Y-m-d H:i:s",time())
                    ]);

                }else {


                    $cc = $capitalc['count_putmoney'] + $dz; //获得

                    if ($cc > $capitalc['putmoney'] && $pid != 1) {
                        $put_out_money = abs($capitalc['putmoney'] - $capitalc['count_putmoney']); //获得
                        $overmoney = $cc - $capitalc['putmoney'];

                        Db::table("t_overflow_log")->insert([
                            "uid" => $pid,
                            "money" => $overmoney,
                            "type"=>1,
                            "addtime" => time(),
                            'created_at' => date("Y-m-d H:i:s", time()),
                            'updated_at' => date("Y-m-d H:i:s", time())
                        ]);

                        Db::table("t_capital")->where("uid", $pid)->where("type", 2)->update([
                            "count_putmoney" => $capitalc['putmoney'],
                            "put_out_money" => $put_out_money
                        ]);
                        Db::table("t_capital_water")->insert([
                            "uid" => $pid,
                            "type" => 8,
                            "cid" => $capitalc['id'],
                            "name" => "用户" . $username . "消费分成" . ($dz) . "收入" . abs($capitalc['putmoney'] - $capitalc['count_putmoney']) . "其中溢出" . $overmoney,
                            "money" => abs($capitalc['putmoney'] - $capitalc['count_putmoney']),
                            "addtime" => time(),
                            "b_username" => $username,
                            "level" => $level,
                            "b_money" => $money,
                            "order_id" => $orderid
                        ]);

                    } else {
                        $put_out_money = $capitalc['put_out_money'] + $dz; //获得
                        Db::table("t_capital")->where("uid", $pid)->where("type", 2)->update([
                            "count_putmoney" => $capitalc['count_putmoney'] + $dz,
                            "put_out_money" => $put_out_money
                        ]);
                        Db::table("t_capital_water")->insert([
                            "uid" => $pid,
                            "type" => 8,
                            "cid" => $capitalc['id'],
                            "name" => "用户" . $username . "消费分成" . $dz,
                            "money" => $dz,
                            "addtime" => time(),
                            "b_username" => $username,
                            "level" => $level,
                            "b_money" => $money,
                            "order_id" => $orderid
                        ]);

                    }
                }
                Db::commit();
                return $pid;
            }else{
                return 0;
            }

        }catch (Exception $e){
            Db::rollback(); //回滚事务
            throw new FailException(['code' => 201,"msg"=>"返利失败","errorCode"=>406]);
            die();
        }


    }
}